package com.welike.shibo.service.impl;

import com.google.gson.JsonObject;
import com.welike.shibo.eneity.PushBean;
import com.welike.shibo.service.JPushService;
import com.welike.shibo.service.MyJPushService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 极光推送
 * 封装业务功能相关
 * @author fangyu
 * @version v1.0.0
 * @since 2019-09-01 12:33
 */
@Service
public class JPushServiceImpl implements JPushService {

    /** 一次推送最大数量 (极光限制1000) */
    private static final int max_size = 800;
//
    @Resource
    private MyJPushService jPushService;

    /**
     * 推送全部, 不支持附加信息
     * @param pushBean 推送Bean
     * @return
     */
    @Override
    public boolean pushAll(PushBean pushBean) {
        return jPushService.pushAll(pushBean);
    }

    /**
     * 推送全部ios
     * @param pushBean
     * @return
     */
    @Override
    public boolean pushIOS(PushBean pushBean) {
        return jPushService.pushIOS(pushBean);
    }

    /**
     * 推送ios 指定id
     * @param pushBean
     * @param registers
     * @return
     */
    @Override
    public boolean pushIOS(PushBean pushBean, Integer pushType, JsonObject jsonObject, String... registers) {
        registers = checkRegisterIds(registers);    // 剔除无效registed
        while (registers.length > max_size){    // 每次推送max_size个
            jPushService.pushIos(pushBean, pushType, jsonObject, Arrays.copyOfRange(registers, 0, max_size));
            registers = Arrays.copyOfRange(registers, max_size, registers.length);
        }
        return jPushService.pushIos(pushBean, pushType,jsonObject, registers);
    }

    /**
     * 推送全部android
     * @param pushBean
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean) {
        return jPushService.pushAndroid(pushBean);
    }

    /**
     * 推送android 指定id
     * @param pushBean
     * @param registers
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean, String... registers) {
        registers = checkRegisterIds(registers);    // 剔除无效registed
        while (registers.length > max_size){    // 每次推送max_size个
            jPushService.pushAndroid(pushBean, Arrays.copyOfRange(registers, 0, max_size));
            registers = Arrays.copyOfRange(registers, max_size, registers.length);
        }
        return jPushService.pushAndroid(pushBean, registers);
    }

    @Override
    public boolean pushAndroid(PushBean pushBean, String tagId) {
        return jPushService.pushAndroid(pushBean, tagId);
    }

    /**
     * 删除无效registed
     * @param registers
     * @return
     */
    @Override
    public String[] checkRegisterIds(String[] registers) {
        List<String> regList = new ArrayList<>(registers.length);
        for (String registid : registers){
            if (registid!=null && !"".equals(registid.trim())){
                regList.add(registid);
            }
        }
        return regList.toArray(new String[0]);
    }
}
